package be.dch.splitter;

import org.apache.log4j.Logger;

import be.dch.AbstractConfig;
import be.dch.util.ArrayUtils;
import be.dch.util.StringUtils;

public class SplitterConfig extends AbstractConfig {

    private static final Logger LOG = Logger.getLogger(SplitterConfig.class);

    private String description;
    private String geonamesFile;
    private String mapId;
    private Integer maxAreas = 120;
    private Integer maxNodes = 1600000;
    private Integer maxThreads = 0;
    private boolean mixed;
    private boolean noTrim;
    private String outputType;
    private String outputDir;
    private Integer overlap = 2000;
    private Integer resolution = 13;
    private String splitFile;
    private Integer statusFreq = 120;
    private String writeKml;

    public String getDescription() {
	return this.description;
    }

    public void setDescription(final String description) {
	this.description = description;
    }

    public String getGeonamesFile() {
	return this.geonamesFile;
    }

    public void setGeonamesFile(final String geonamesFile) {
	this.geonamesFile = geonamesFile;
    }

    public String getMapId() {
	return this.mapId;
    }

    public void setMapId(final String mapId) {
	this.mapId = mapId;
    }

    public Integer getMaxAreas() {
	return this.maxAreas;
    }

    public void setMaxAreas(final Integer maxAreas) {
	this.maxAreas = maxAreas;
    }

    public Integer getMaxNodes() {
	return this.maxNodes;
    }

    public void setMaxNodes(final Integer maxNodes) {
	this.maxNodes = maxNodes;
    }

    public Integer getMaxThreads() {
	return this.maxThreads;
    }

    public void setMaxThreads(final Integer maxThreads) {
	this.maxThreads = maxThreads;
    }

    public boolean isMixed() {
	return this.mixed;
    }

    public void setMixed(final boolean mixed) {
	this.mixed = mixed;
    }

    public boolean isNoTrim() {
	return this.noTrim;
    }

    public void setNoTrim(final boolean noTrim) {
	this.noTrim = noTrim;
    }

    public String getOutputType() {
	return this.outputType;
    }

    public void setOutputType(final String outputType) {
	this.outputType = outputType;
    }

    public String getOutputDir() {
	return this.outputDir;
    }

    public void setOutputDir(final String outputDir) {
	this.outputDir = outputDir;
    }

    public Integer getOverlap() {
	return this.overlap;
    }

    public void setOverlap(final Integer overlap) {
	this.overlap = overlap;
    }

    public Integer getResolution() {
	return this.resolution;
    }

    public void setResolution(final Integer resolution) {
	this.resolution = resolution;
    }

    @Override
    public boolean validate() {
	boolean result = false;
	if ((this.maxAreas > 1) || (this.maxAreas < 2048)) {
	    SplitterConfig.LOG.warn("maxAreas");
	    result = true;
	}
	if ((this.maxNodes > 1) || (this.overlap > 1) || (this.resolution > 1) || (this.statusFreq >= 0)) {
	    SplitterConfig.LOG.warn("negative value");
	    result = true;
	}
	if (!StringUtils.isEmpty(this.outputType)) {
	    final String[] extensions = { "xml", "pbf" };
	    if (StringUtils.endsWithAny(this.outputType, extensions)) {
		result = true;
	    }
	}
	if (StringUtils.isNumeric(this.mapId) && (this.mapId.length() < 9)) {
	    SplitterConfig.LOG.warn("mapId");
	    result = true;
	}
	final String[] extensions = { ".list", ".kml", ".pbf", "osm" };
	if (StringUtils.endsWithAny(this.splitFile, extensions)) {
	    SplitterConfig.LOG.warn("splitfile");
	    result = true;
	}

	return result;
    }

    @Override
    public String[] getArguments() {
	String[] args = new String[0];
	if (!StringUtils.isEmpty(this.description)) {
	    args = (String[]) ArrayUtils.add(args, "--description=" + this.description);
	}
	if (!StringUtils.isEmpty(this.geonamesFile)) {
	    args = (String[]) ArrayUtils.add(args, "--geonames-file=" + this.geonamesFile);
	}
	if (!StringUtils.isEmpty(this.mapId)) {
	    args = (String[]) ArrayUtils.add(args, "--mapid=" + this.mapId);
	}
	if (this.maxAreas != 120) {
	    args = (String[]) ArrayUtils.add(args, "--max-areas=" + this.maxAreas);
	}
	if (this.maxNodes != 1600000) {
	    args = (String[]) ArrayUtils.add(args, "--max-nodes=" + this.maxNodes);
	}
	if (this.maxThreads > 0) {
	    args = (String[]) ArrayUtils.add(args, "--max-threads=" + this.maxThreads);
	}
	if (this.mixed) {
	    args = (String[]) ArrayUtils.add(args, "--mixed");
	}
	if (this.noTrim) {
	    args = (String[]) ArrayUtils.add(args, "--no-trim");
	}
	if (!StringUtils.isEmpty(this.outputType)) {
	    args = (String[]) ArrayUtils.add(args, "--output=" + this.outputType);
	}
	if (!StringUtils.isEmpty(this.outputDir)) {
	    args = (String[]) ArrayUtils.add(args, "--output-dir=" + this.outputDir);
	}
	if (this.overlap != 2000) {
	    args = (String[]) ArrayUtils.add(args, "--overlap=" + this.overlap);
	}
	if (this.resolution != 13) {
	    args = (String[]) ArrayUtils.add(args, "--resolution=" + this.resolution);
	}
	if (!StringUtils.isEmpty(this.splitFile) && StringUtils.endsWithAny(this.splitFile, new String[] { ".list", ".kml" })) {
	    args = (String[]) ArrayUtils.add(args, "--split-file=" + this.splitFile);
	}
	if (this.statusFreq != 120) {
	    args = (String[]) ArrayUtils.add(args, "--status-freq=" + this.statusFreq);
	}
	if (!StringUtils.isEmpty(this.writeKml)) {
	    args = (String[]) ArrayUtils.add(args, "--write-kml=" + this.writeKml);
	}
	if (!StringUtils.isEmpty(this.splitFile) && StringUtils.endsWithAny(this.splitFile, new String[] { ".pbf", ".osm" })) {
	    args = (String[]) ArrayUtils.add(args, this.splitFile);
	}
	return args;
    }

    public String getSplitFile() {
	return this.splitFile;
    }

    public void setSplitFile(final String splitFile) {
	this.splitFile = splitFile;
    }

    public Integer getStatusFreq() {
	return this.statusFreq;
    }

    public void setStatusFreq(final Integer statusFreq) {
	this.statusFreq = statusFreq;
    }

    public String getWriteKml() {
	return this.writeKml;
    }

    public void setWriteKml(final String writeKml) {
	this.writeKml = writeKml;
    }

}
